//
// Created by jsjxsy on 2022/9/14.
//

#include "workerManager.h"
#include <Employee.h>
#include <Manager.h>
#include <Boss.h>
#include <fstream>
#include <algorithm>
using namespace std;
#define FILE_NAME "work.txt"
WorkerManager::WorkerManager() {
  ifstream ifs;
  ifs.open(FILE_NAME);
  if (!ifs.is_open()) {
    cout << "文件不存在" << endl;
    this->m_number = 0;
    this->m_workers = nullptr;
    this->m_FileIsEmpty = true;
    ifs.close();
    return;
  }
  char ch;
  ifs >> ch;
  if (ifs.eof()) {
    cout << "文件为空" << endl;
    this->m_number = 0;
    this->m_workers = nullptr;
    this->m_FileIsEmpty = true;
    ifs.close();
    return;
  }
  this->m_number = Get_Number();
  if (this->m_number > 0) {
    this->m_FileIsEmpty = false;
    this->m_workers = new Worker *[this->m_number];
    Init_Emp();
  }
}
void WorkerManager::Show_Menu() {
  cout << "******************************************" << endl;
  cout << "**************欢迎使用员工系统***************" << endl;
  cout << "*************  0.退出管理程序  *************" << endl;
  cout << "*************  1.增加职工信息  *************" << endl;
  cout << "*************  2.显示职工信息  *************" << endl;
  cout << "*************  3.删除离职职工  *************" << endl;
  cout << "*************  4.修改职工信息  *************" << endl;
  cout << "*************  5.查找职工信息  *************" << endl;
  cout << "*************  6.按照编号排序  *************" << endl;
  cout << "*************  7.清空所有文档  *************" << endl;
  cout << "******************************************" << endl;
  cout << endl;
}
void WorkerManager::Exit_System() {
  cout << "欢迎下次使用" << endl;
  exit(0);
}

void WorkerManager::Add_Worker() {
  cout << "请输入增加职工数量:" << endl;
  int addNum = 0;
  cin >> addNum;
  if (addNum > 0) {
    int size = m_number + addNum;
    Worker **new_add_workers = new Worker *[size];
//    if (new_add_workers == nullptr) {
//      cerr << "内存空间不足" << endl;
//      exit(-1);
//    }
    if (m_workers != nullptr) {
      for (int i = 0; i < m_number; i++) {
        new_add_workers[i] = m_workers[i];
      }
    }
    for (int i = 0; i < addNum; i++) {
      int id;
      string name;
      int deptId;
      int dSelect;
      cout << "请输入第 " << i + 1 << " 个新职工编号：" << endl;
      cin >> id;

      cout << "请输入第 " << i + 1 << " 个新职工名称：" << endl;
      cin >> name;

      cout << "请输入第 " << i + 1 << " 个新职工部门编号：" << endl;
      cin >> deptId;

      cout << "请选择该职工的岗位：" << endl;
      cout << "1、普通职工" << endl;
      cout << "2、经理" << endl;
      cout << "3、老板" << endl;
      cin >> dSelect;
      Worker *worker = nullptr;
      switch (dSelect) {
        case 1:worker = new Employee(id, name, deptId);
          break;
        case 2:worker = new Manager(id, name, deptId);
          break;
        case 3:worker = new Boss(id, name, deptId);
          break;
      }
      new_add_workers[m_number + i] = worker;
    }

    delete[] this->m_workers;
    this->m_workers = new_add_workers;
    this->m_number = size;
    this->m_FileIsEmpty = false;
    Save_ToFile();
    cout << "成功添加" << addNum << "名新职工！" << endl;
  } else {
    cout << "输入有误" << endl;
  }
  system("clear");
}

void WorkerManager::Save_ToFile() {
  ofstream ofs;
  ofs.open(FILE_NAME, ios_base::out);
  if (m_workers != nullptr) {
    for (int i = 0; i < m_number; i++) {
      ofs << this->m_workers[i]->m_Id << " "
          << this->m_workers[i]->m_Name << " "
          << this->m_workers[i]->m_DeptId << endl;
    }
  }
  ofs.close();
}
void WorkerManager::Show_Worker() {
  if (m_FileIsEmpty) {
    cout << "文件不存在或记录为空！" << endl;
  } else {
    if (m_workers != nullptr) {
      for (int i = 0; i < m_number; i++) {
        this->m_workers[i]->Show_Information();
      }
    }
  }
}

void WorkerManager::Delete_Worker() {
  if (m_FileIsEmpty) {
    cout << "文件不存在或者记录为空" << endl;
  } else {
    cout << "请输入要删除员工的Id:" << endl;
    int id;
    cin >> id;
    int index = Is_Exist(id);
    if (index != -1) {
      for (int i = index; i < m_number; i++) {
        m_workers[i] = m_workers[i + 1];
      }
      m_number--;
      Save_ToFile();
      cout << "删除员工成功" << endl;
    } else {
      cout << "删除员工失败" << endl;
    }
  }

}

void WorkerManager::Find_Worker() {
  if (m_FileIsEmpty) {
    cout << "文件不存在或者记录为空" << endl;
  } else {
    cout << "请输入查找的方式：" << endl;
    cout << "1、按职工编号查找" << endl;
    cout << "2、按姓名查找" << endl;
    int select;
    cin >> select;
    switch (select) {
      case 1:Find_ByNo();
        break;
      case 2:Find_ByName();
        break;
    }
  }
}

void WorkerManager::Find_ByNo() {
  cout << "请输入职工编号:" << endl;
  int id;
  cin >> id;
  int index = -1;
  for (int i = 0; i < m_number; i++) {
    if (m_workers[i]->m_Id == id) {
      index = i;
      break;
    }
  }

  if (index != -1) {
    cout << "编号为" << id << "的员工信息如下:" << endl;
    m_workers[index]->Show_Information();
  } else {
    cout << "没有找到编号为：" << id << "的员工" << endl;
  }
}

void WorkerManager::Find_ByName() {
  cout << "请输入职工姓名:" << endl;
  string name;
  cin >> name;
  int index = -1;
  for (int i = 0; i < m_number; i++) {
    if (m_workers[i]->m_Name == name) {
      index = i;
      break;
    }
  }

  if (index != -1) {
    cout << "姓名为" << name << "的员工信息如下:" << endl;
    m_workers[index]->Show_Information();
  } else {
    cout << "没有找到姓名为：" << name << "的员工" << endl;
  }
}

void WorkerManager::Modify_Worker() {
  if (m_FileIsEmpty) {
    cout << "文件不存在或者记录为空" << endl;
  } else {
    cout << "请输入要修改员工的Id:" << endl;
    int id;
    cin >> id;
    int index = Is_Exist(id);
    if (index != -1) {
      delete this->m_workers[index];
      int newId;
      string newName;
      int newDeptId;
      cout << "请输入职工新编号：" << endl;
      cin >> newId;

      cout << "请输入职工新名称：" << endl;
      cin >> newName;

      cout << "请输入职工部门新编号：" << endl;
      cin >> newDeptId;

      int dSelect;
      cout << "请选择该职工的岗位：" << endl;
      cout << "1、普通职工" << endl;
      cout << "2、经理" << endl;
      cout << "3、老板" << endl;
      cin >> dSelect;
      Worker *worker = nullptr;
      switch (dSelect) {
        case 1:worker = new Employee(newId, newName, newDeptId);
          break;
        case 2:worker = new Manager(newId, newName, newDeptId);
          break;
        case 3:worker = new Boss(newId, newName, newDeptId);
          break;
      }
      this->m_workers[index] = worker;
      cout << "修改成功" << endl;
      Save_ToFile();
    } else {
      cout << "编号为:" << id << "不存在" << endl;
    }
  }
}

void WorkerManager::Sort_ByNumber() {
  if (this->m_FileIsEmpty) {
    cout << "文件不存在或记录为空！" << endl;
    system("pause");
    system("cls");
  } else {
    cout << "请选择排序方式： " << endl;
    cout << "1、按职工号进行升序" << endl;
    cout << "2、按职工号进行降序" << endl;
    int select = 0;
    cin >> select;

    for (int i = 0; i < m_number; i++) {
      int minOrMax = i;
      for (int j = i + 1; j < m_number; j++) {
        if (select == 1) //升序
        {
          if (m_workers[minOrMax]->m_Id > m_workers[j]->m_Id) {
            minOrMax = j;
          }
        } else  //降序
        {
          if (m_workers[minOrMax]->m_Id < m_workers[j]->m_Id) {
            minOrMax = j;
          }
        }
      }

      if (i != minOrMax) {
        Worker *temp = m_workers[i];
        m_workers[i] = m_workers[minOrMax];
        m_workers[minOrMax] = temp;
      }

    }
    cout << "排序成功,排序后结果为：" << endl;
    this->Save_ToFile();
    this->Show_Worker();
  }
}

void WorkerManager::clear() {
  cout << "确认清空？" << endl;
  cout << "1、确认" << endl;
  cout << "2、返回" << endl;

  int select = 0;
  cin >> select;

  if (select == 1) {
    //打开模式 ios::trunc 如果存在删除文件并重新创建
    ofstream ofs(FILE_NAME, ios::trunc);
    ofs.close();

    if (this->m_workers != NULL) {
      for (int i = 0; i < this->m_number; i++) {
        if (this->m_workers[i] != NULL) {
          delete this->m_workers[i];
        }
      }
      this->m_number = 0;
      delete[] this->m_workers;
      this->m_workers = NULL;
      this->m_FileIsEmpty = true;
    }
    cout << "清空成功！" << endl;
  }
}

int WorkerManager::Is_Exist(int id) {
  int index = -1;
  for (int i = 0; i < m_number; i++) {
    if (m_workers[i]->m_Id == id) {
      index = i;
      break;
    }
  }
  return index;
}

int WorkerManager::Get_Number() {
  ifstream ifs;
  ifs.open(FILE_NAME);
  int id;
  string name;
  int deptId;
  int num = 0;
  while (ifs >> id && ifs >> name && ifs >> deptId) {
    num++;
  }
  ifs.close();
  return num;
}

void WorkerManager::Init_Emp() {
  ifstream ifs;
  ifs.open(FILE_NAME, ios_base::in);
  int id;
  string name;
  int deptId;
  int index = 0;
  while (ifs >> id && ifs >> name && ifs >> deptId) {
    Worker *worker = nullptr;
    switch (deptId) {
      case 1:worker = new Employee(id, name, deptId);
        break;
      case 2:worker = new Manager(id, name, deptId);
        break;
      case 3:worker = new Boss(id, name, deptId);
        break;
    }
    this->m_workers[index] = worker;
    index++;
  }
  ifs.close();
}
WorkerManager::~WorkerManager() {
  if (m_workers != nullptr) {
    delete[] m_workers;
    m_workers = nullptr;
  }
}